<?php

class Mac_Db_Query_Delete extends Mac_Db_Query_Abstract
{
    private $table = null;
    
    private $whereClause = array();
    
    private $order = null;
    
    private $limit = null;
    
    private function __construct ($table)
    {
        $this->table = $table;
    }
    
    /**
     * Initialize the query
     */
    public static function from ($table)
    {
        return new self($table);
    }
    
    public function where (Mac_Db_Clause_Where $where)
    {
        $this->whereClause = array($where->__toString());
        return $this;
    }
    
    public function orWhere (Mac_Db_Clause_Where $where)
    {
        $this->whereClause[] = $where->__toString();
        return $this;
    }
    
    public function orderBy ($order)
    {
        $this->order = $order;
        return $this;
    }
    
    public function limit ($limit)
    {
        $this->limit = $limit;
        return $this;
    }
    
    public function __toString()
    {
        $req = 'DELETE FROM '.$this->table;
    
        if (!empty($this->whereClause)) {
            $req .= " WHERE ".Mac_Db_Helper::outputWhere($this->whereClause);
        }
    
        if (isset($this->order)) {
            $req .= " ORDER BY ".Mac_Db_Helper::outputOrderBy($this->order);
        }
    
        if ($this->limit !== null) {
            $req .= ' LIMIT '.$this->limit;
        }
    
        return $req;
    }
}